package com.czh.springboot04.service.serviceimpl;

import com.czh.springboot04.entity.Charge;
import com.czh.springboot04.entity.ChargeType;
import com.czh.springboot04.entity.Consult;
import com.czh.springboot04.entity.ConsultConti;
import com.czh.springboot04.mapper.ChargeMapper;
import com.czh.springboot04.mapper.ChargeTypeMapper;
import com.czh.springboot04.mapper.ConsultContiMapper;
import com.czh.springboot04.mapper.ConsultMapper;
import com.czh.springboot04.service.ConsultService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ConsultServiceImpl implements ConsultService {
    @Autowired
    private ConsultMapper consultMapper;
    @Autowired
    private ConsultContiMapper consultContiMapper;

    @Override
    public int add(Consult consult) {
        return consultMapper.insertConsult(consult);
    }

    @Override
    public int edit(Consult consult) {
        return consultMapper.updateConsult(consult);
    }

    @Override
    public int delete(int id) {
        return consultMapper.deleteConsult(id);
    }

    @Override
    public Consult findById(int id) {
        return consultMapper.findConsultById(id);
    }

    @Override
    public List<Consult> findAllReally() {
        return consultMapper.findAllReally();
    }

    @Override
    public PageInfo<Consult> findAll(int pageNum, int pageSize, Consult consult) {
        PageHelper.startPage(pageNum, pageSize);
        List<Consult> list = consultMapper.findAll(consult);
        for (Consult consult1 : list) {
            ConsultConti consultConti = consultContiMapper.selectById(consult1.getId());
            consult1.setConsultConti(consultConti);
        }
        return new PageInfo<>(list);
    }

    @Override
    public Consult findConsultByName(String name) {
        Consult consult = consultMapper.findConsultByName(name);
        return consult;
    }

    @Override
    public List<HashMap<String, Object>> getCount() {
        List<HashMap<Integer, Object>> list = consultMapper.getCount();
        HashMap<String, Object> map = new HashMap<>();
        ////System.out.println(list);
        List list1 = new ArrayList();
        int i = 0;
        if (list != null && !list.isEmpty()) {
            //System.out.println(list);
            for (HashMap<Integer, Object> hashMap : list) {
                Integer key = 0;
                String name = "";
                Integer value = 0;
                for (Map.Entry<Integer, Object> entry : hashMap.entrySet()) {
                    if (i == 0) {
                        map = new HashMap<>();
                    }
                    i++;
                    //System.out.println(entry.getKey());
                    if ("name".equals(entry.getKey())) {
                        key = (Integer) entry.getValue();
                        if (key == 1) {
                            name = "未认证";
                        } else if (key == 2) {
                            name = "已认证";
                        }
                        map.put("name", name);
                    } else if ("value".equals(entry.getKey())) {
                            //我需要的是int型所以做了如下转换，实际上返回的object应为Long。
                            value = ((Long) entry.getValue()).intValue();
                            map.put("value", value);
                    }
                    if (i == 2) {
                        list1.add(map);
                        i = 0;
                    }
                }
            }
            //System.out.println(list1);
        }
        return list1;
    }

    @Override
    public PageInfo<Consult> findAllByUserId(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Consult> list = consultMapper.findAllReally();
        return new PageInfo<>(list);
    }

}
